package com.yuxl.common.auth.service;

import com.yuxl.common.auth.entity.AuthMenu;
import com.yuxl.common.auth.entity.AuthRoleMenu;
import com.yuxl.common.auth.entity.AuthUserRole;
import com.yuxl.common.auth.repository.AuthRoleMenuRepository;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @author yuxl
 */
@Slf4j
@Service
public class AuthRoleMenuService {
    @Resource
    private AuthRoleMenuRepository authRoleMenuRepository;

    public Flux<AuthMenu> findByRoleMark(String roleMark) {
        return authRoleMenuRepository.findByRoleMark(roleMark);
    }

    public Flux<AuthMenu> findByRoleMarks(List<String> roleMarks) {
        return authRoleMenuRepository.findByRoleMarks(roleMarks);
    }

    public Flux<AuthRoleMenu> save(String roleMark, List<String> menuNames) {
        if (menuNames == null || menuNames.isEmpty()) {
            return Flux.empty();
        }
        List<AuthRoleMenu> list = menuNames.stream().map(menuName -> new AuthRoleMenu().setMenuName(menuName).setRoleMark(roleMark)).toList();
        return authRoleMenuRepository.deleteByRoleMark(roleMark)
                .flatMapMany(k->authRoleMenuRepository.saveAll(list));
    }

    public Mono<Long> deleteByRoleMark(String roleMark) {
        return authRoleMenuRepository.deleteByRoleMark(roleMark);
    }

}